package br.cefet.farmaciaonline.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.cefet.farmaciaonline.connection.ConnectionFactory;
import br.cefet.farmaciaonline.vo.Paciente;


public class CompraDAO {
	Connection con = null;
	PreparedStatement ptmt = null;
	ResultSet rs = null;

	/*
	 * public EmployeeJdbcDAO() {
	 * 
	 * }
	 */

	private Connection getConnection() throws SQLException {
		Connection conn;
		conn = ConnectionFactory.getInstance().getConnection();
		return conn;
	}

	public void add(Paciente paciente) {

		try {
			String querystring = "INSERT INTO EMPLOYEE VALUES(?,?,?,?,?)";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			/*
			 * ptmt.setString(1, String.valueOf(System.currentTimeMillis()));
			 * ptmt.setString(2, paciente.getEmpName()); ptmt.setString(3,
			 * paciente.getEmpAddr()); ptmt.setString(4,
			 * paciente.getEmpEmail()); ptmt.setString(5,
			 * paciente.getEmpPhone());
			 */
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public void update(Paciente paciente) {

		try {
			String querystring = "UPDATE EMPLOYEE SET EMP_NAME=?,EMP_ADDR=?,"
					+ "EMP_EMAIL=?,EMP_PHONE=? WHERE EMP_ID=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);

			/*
			 * ptmt.setString(1, employeeBean.getEmpName()); ptmt.setString(2,
			 * employeeBean.getEmpAddr()); ptmt.setString(3,
			 * employeeBean.getEmpEmail()); ptmt.setString(4,
			 * employeeBean.getEmpPhone()); ptmt.setString(5,
			 * employeeBean.getEmpId());
			 */
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public void delete(String employeeId) {

		try {
			String querystring = "DELETE FROM EMPLOYEE WHERE EMPID=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			ptmt.setString(1, employeeId);
			ptmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public List<Paciente> findAll() {
		List<Paciente> pacientes = new ArrayList<Paciente>();
		Paciente paciente = null;
		try {
			String querystring = "SELECT * FROM EMPLOYEE";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			rs = ptmt.executeQuery();
			while (rs.next()) {
				paciente = new Paciente();
				/*
				 * employeeBean.setEmpId(rs.getString(1)); Trocar por paciente
				 * employeeBean.setEmpName(rs.getString(2));
				 * employeeBean.setEmpAddr(rs.getString(3));
				 * employeeBean.setEmpEmail(rs.getString(4));
				 * employeeBean.setEmpPhone(rs.getString(5));
				 */

				pacientes.add(paciente);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return pacientes;
	}

	public Paciente findByPrimaryKey(String empId) {

		Paciente paciente = null;
		try {
			String querystring = "SELECT * FROM EMPLOYEE WHERE EMP_ID=?";
			con = getConnection();
			ptmt = con.prepareStatement(querystring);
			ptmt.setString(1, empId);
			rs = ptmt.executeQuery();
			if (rs.next()) {
				/*
				 * employeeBean=new EmployeeBean(); trocar por paciente
				 * employeeBean.setEmpId(rs.getString(1));
				 * employeeBean.setEmpName(rs.getString(2));
				 * employeeBean.setEmpAddr(rs.getString(3));
				 * employeeBean.setEmpEmail(rs.getString(4));
				 * employeeBean.setEmpPhone(rs.getString(5));
				 */
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ptmt != null)
					ptmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return paciente;
	}

	public static void main(String[] args) {

		PacienteDAO employeeDAO = new PacienteDAO();
		System.out.println(employeeDAO.findAll().size());
	}
}